/*
 * user_it.c
 *
 *  Created on: Nov 2, 2021
 *      Author: zz
 */

#include "main.h"
#include "stm32f1xx_it.h"
#include "servo.h"

extern TIM_HandleTypeDef htim3;

static __IO uint32_t tick = 0;

__IO uint32_t get_tim3_ms()
{
	return tick + htim3.Instance->CNT/1000;
}

__IO uint32_t get_tim3_us()
{
	uint32_t us = htim3.Instance->CNT % 1000;
	return us;
}

void tim3_delayus(uint32_t us)
{
	uint32_t  gapTim = htim3.Instance->CNT & 0x0000ffff;
	uint32_t  gapTimMs = tick;

	gapTim+=us;

	if( gapTim < 20000 )
	{
		while(htim3.Instance->CNT < gapTim);
		return;
	}
	else
	{
		while( !((gapTimMs!=tick)&&(htim3.Instance->CNT>=(gapTim-20000)) ));
		return;
	}
}

void TIM3_PeriodElapsedCallback()
{
  tick += 20;

  //舵机Timer Update事件
  servo_tim_periodelapsed();
}

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
	if(htim == &htim3) {
		//舵机PWM 事件
		servo_pwm_pulsefinished(htim);
	}
}
